package com.huawei.hms.framework.network.restclient.hwhttp.dns.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.NetworkUtil;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.cache.CacheManager;
import com.huawei.hms.framework.network.util.ContextUtil;
import com.huawei.hwid.core.constants.HwAccountConstants;

public class NetworkStateReceiver extends BroadcastReceiver {
    private static final String TAG = "NetworkStateReceiver";
    private NetworkInfo lastActivityNetInfo = NetworkUtil.getNetworkInfo(ContextUtil.getContext());

    NetworkStateReceiver() {
        String str;
        String str2 = TAG;
        String str3 = "lastActivityNetInfo: %s";
        Object[] objArr = new Object[1];
        if (this.lastActivityNetInfo == null) {
            str = HwAccountConstants.NULL;
        } else {
            str = this.lastActivityNetInfo.toString();
        }
        objArr[0] = str;
        Logger.v(str2, str3, objArr);
    }

    public static void registerNetworkState(Context context) {
        if (context == null) {
            Logger.w(TAG, "invalid parameter");
            return;
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        context.registerReceiver(new NetworkStateReceiver(), intentFilter);
        Logger.v(TAG, "Register Network State Listen Success!");
    }

    private int getNetworkType(NetworkInfo networkInfo) {
        switch (NetworkUtil.getNetworkType(networkInfo)) {
            case 1:
                return 1;
            case 2:
            case 3:
            case 4:
                return 2;
            default:
                return 0;
        }
    }

    private boolean isConnectTypeChange(NetworkInfo networkInfo, NetworkInfo networkInfo2) {
        if (networkInfo == null || !networkInfo.isConnected() || !networkInfo2.isConnected() || getNetworkType(networkInfo) == getNetworkType(networkInfo2)) {
            return false;
        }
        Logger.v(TAG, "Find activity network changed");
        return true;
    }

    private boolean isChangeToConnected(NetworkInfo networkInfo, NetworkInfo networkInfo2) {
        if ((networkInfo != null && networkInfo.isConnected()) || !networkInfo2.isConnected()) {
            return false;
        }
        Logger.v(TAG, "Find network state changed to connected");
        return true;
    }

    private boolean isNeedToUpdateDnsMemoryCache(NetworkInfo networkInfo, NetworkInfo networkInfo2) {
        if (isChangeToConnected(networkInfo, networkInfo2) || isConnectTypeChange(networkInfo, networkInfo2)) {
            return true;
        }
        return false;
    }

    public void onReceive(Context context, Intent intent) {
        Logger.v(TAG, "Capture network state change");
        NetworkInfo networkInfo = NetworkUtil.getNetworkInfo(context);
        if (networkInfo == null) {
            Logger.v(TAG, "Get NetworkInfo failed");
            return;
        }
        Logger.v(TAG, "networkInfo: %s", networkInfo);
        if (isNeedToUpdateDnsMemoryCache(this.lastActivityNetInfo, networkInfo)) {
            CacheManager.onNetworkChange(networkInfo);
            this.lastActivityNetInfo = networkInfo;
        }
    }
}
